#!/usr/bin/python
#encoding=utf-8

import os
import Queue
from wm.etc.config import CLUSTER
from wm.lib.loglib import log_error
from wm.db.TransactionManager import TransactionManager

def get_website_list(cluster):
    '''
    return all websites
    '''
    tran = TransactionManager()
    try:
        tran.startTransaction()
        sql = "select id,url,websiteName,tcpPortSettings,udpPortSettings,downloadSpeed,loadTimeThreshold,responseTimeThreshold,"+\
                            "downloadSpeedFrequency,loadTimeThresholdFrequency,responseTimeThresholdFrequency,portFrequency,senWordFrequency,tamperFrequency from websiteconfiginfo where cluster=" + str(cluster)
        res = tran.select(sql)
        tran.commitTransaction()
        
        if res:
            return res
        else:
            return None
    except Exception as e:
            log_error("get_website_list():"+str(e))
            tran.rollbackTransaction()
    finally:
        tran.close()

def update_recent_alarmevent(tran,websiteid,websiteName,alertCategory,alarmcontent):
    '''
    update recent alarmevent
    @tran transaction 
    '''
    try:
        sql = "select * from recentalarmevent where websiteConfigInfo_id = '"+ websiteid + "' and alertCategory = '" + alertCategory + "'"
        recentevent = tran.select(sql)
        
        if recentevent:
            sql = "update recentalarmevent set modifyDate=CURRENT_TIMESTAMP(),occurrenceDate=CURRENT_TIMESTAMP(),alertContent='"+alarmcontent+"'" + " where id ='" +recentevent[0][0]+"'"
            tran.update(sql)
        else:
            sql = "insert into recentalarmevent(id,createDate,modifyDate,alertCategory,alertContent,occurrenceDate,websiteName,websiteConfigInfo_id)" +\
                    "values(replace(uuid(),'-',''),CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),'" + alertCategory +  "','" + alarmcontent + "',CURRENT_TIMESTAMP(),"+\
                        "'" + websiteName + "','" + websiteid + "')"
            tran.execute(sql)
    except:
        print "update recent event except"
        raise

def get_pageanalyzes():
    tran = TransactionManager()
    try:
        sql = "select pa.id,pa.uRLType,pa.elementURL,pa.local,wc.websitename,pa.websiteConfigInfo_id,wc.tamperFrequency  from pageanalyzer as pa,websiteconfiginfo as wc where pa.websiteConfigInfo_id = wc.id and wc.cluster="+str(CLUSTER['cluster'])
        palist = tran.select(sql) 
        tran.commitTransaction()
        
        if palist:
            return palist
        else:
            return None
    except:
        tran.rollbackTransaction()
    finally:
        tran.close()
    
def monitor_item_inqueue(items):
    qitem = Queue.Queue()
    for item in  items:
        qitem.put(item)
    return qitem

def monitor_item_outqueue(qitem):
    try:
        if qitem.qsize() != 0:
            item = qitem.get(block = False)
        else:
            item = None
        return item
    except Exception as e:
        print str(e)
        return None


        
if __name__ == "__main__":  
    print get_pageanalyzes()
    '''
    sitelist = get_website_list()
    if sitelist:
        for row in sitelist:
            print str(float('%.3f' %row[5]))
    '''